<template>
  <transition name="popup-slide-up">
    <div class="ebook-popup-list" v-show="fontFamilyVisible">
      <div class="ebook-popup-title">
        <div class="ebook-popup-title-icon" @click="hide">
          <span class="icon-down2"></span>
        </div>
        <span class="ebook-popup-title-text">{{$t('book.selectFont')}}</span>
      </div>
      <div class="ebook-popup-list-wrapper">
        <div
          class="ebook-popup-item"
          v-for="(family, index) of fontFamilyList"
          :key="'family' + index"
          @click="setFontFamily(family.font)"
        >
          <div class="ebook-popup-item-text" :class="{'selected': isSelected(family)}">
            {{family.font}}
          </div>
          <div class="ebook-popup-item-check" v-if="isSelected(family)">
            <span class="icon-check"></span>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { defineComponent } from 'vue'
import { useStore } from 'vuex'
import { ebookMixin } from '@/mixins/mixin'
import { FONT_FAMILY } from '@/utils/book'
import { saveFontFamily } from '@/utils/localStorage'
export default defineComponent({
  mixins: [ebookMixin],
  name: 'EbookSettingFontPopup',
  setup () {
    const store = useStore()
    // 隐藏弹框
    const hide = () => {
      store.dispatch('book/setFontFamilyVisible', false)
    }
    /**
     *@param:
     *@description: 是否选中
     *@author: Mister Wang
     *@date: 2021-06-06 13:26:35
    */
    const isSelected = (family) => {
      const defaultFontFamily = store.getters['book/defaultFontFamily']
      if (defaultFontFamily === family.font) return true
    }
    /**
     *@param: 参数
     *@description: 字体设置
     *@author: Mister Wang
     *@date: 2021-06-06 13:33:39
    */
    const setFontFamily = (font) => {
      store.dispatch('book/setDefaultFontFamily', font)
      saveFontFamily(store.getters['book/fileName'], font)
      const newBook = store.state.book.currentBook
      if (font === 'Default') {
        newBook.rendition.themes.font('Times New Roman')
      } else {
        newBook.rendition.themes.font(font)
      }
      store.dispatch('book/setCurrentBook', newBook)
    }
    return {
      hide,
      isSelected,
      setFontFamily,
      fontFamilyList: FONT_FAMILY
    }
  }
})
</script>
<style lang="scss" scoped>
.ebook-popup-list {
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 300;
    width: 100%;
    background: white;
    box-shadow: 0 px2rem(-4) px2rem(6) rgba(0, 0, 0, .1);
    .ebook-popup-title {
      position: relative;
      padding: px2rem(15);
      box-sizing: border-box;
      border-bottom: px2rem(1) solid #b8b9bb;
      text-align: center;
      @include center;
      .ebook-popup-title-icon {
        position: absolute;
        left: px2rem(15);
        top: 0;
        height: 100%;
        font-size: px2rem(16);
        font-weight: bold;
        @include center;
      }
      .ebook-popup-title-text {
        font-size: px2rem(14);
        font-weight: bold;
      }
    }
    &-wrapper {
      .ebook-popup-item {
        display: flex;
        padding: px2rem(15);
        .ebook-popup-item-text {
          flex: 1;
          font-size: px2rem(14);
          text-align: left;
          &.selected {
            color: #346cb9;
            font-weight: bold;
          }
        }
        .ebook-popup-item-check {
          flex: 1;
          text-align: right;
          font-size: px2rem(14);
          font-weight: bold;
          color: #346cb9;
        }
      }
    }
}
</style>
